﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Task.Logic;
using Task.Common.Entity;
using System.Web.UI.HtmlControls;
using Task.Common.Interface;
using OC.Common.Entity;
using Common.Service;
using Common.BasePages;

namespace Task
{
    public partial class SearchUser : BasePage
    {
        private IUser _IUser;
        public IUser IUser 
        {
            get 
            {
                if (_IUser == null) 
                {
                    _IUser = ServiceContainer.GetService<IUser>();
                }
                return _IUser;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                BindList(0);
            }
        }

        public void BindList(int index) 
        {
            int pageSize = 10;
            PagedDataSource page = new PagedDataSource();
            string whereStr = " where 1=1";
            if (!string.IsNullOrEmpty(txtUserName.Value)) 
            {
                whereStr += " and CN_NAME like '%"+txtUserName.Value+"%'";
            }
            if (!string.IsNullOrEmpty(txtUserCode.Value)) 
            {
                whereStr += " and USER_CODE like '%"+txtUserCode.Value+"%'";
            }
            List<OC.Common.Entity.UserEntity> list = IUser.GetList(whereStr);
            int totalCount = list.Count;
            page.DataSource = list;
            page.AllowPaging = true;
            page.PageSize = pageSize;
            page.CurrentPageIndex = index;

            rptUserList.DataSource = page;
            rptUserList.DataBind();

            pagination.RecordCount = totalCount;
            pagination.PageSize = page.PageSize;
            pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
            pagination.Visible = true;
            pagination.AlwaysShow = true;
            pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
            pagination.DataBind();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(0);
        }

        protected void pagination_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex - 1);
        }

        protected void btnChoose_Click(object sender, EventArgs e)
        {
            string type = Request["type"].ToString();
            foreach (RepeaterItem items in rptUserList.Items) 
            {
                HtmlInputRadioButton btn = (HtmlInputRadioButton)items.FindControl("cboUser");
                if (btn.Checked) 
                {
                    string userId = ((HiddenField)items.FindControl("hidUserID")).Value;
                    string userName = ((HiddenField)items.FindControl("hidUserName")).Value;
                    string returnValue = userId.Trim() + "," + userName.Trim() + "," + type;
                    //Page.RegisterClientScriptBlock("callback", "<script>parent.CallBack('" + userId.Trim() + "','" + userName.Trim() + "','" + type + "'); window.close();</script>");
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "callback", "<script>window.returnValue='" + returnValue + "';window.close();</script>");
                }
            }
            //Page.RegisterClientScriptBlock("callback", "<script>parent.back();</script>");
        }
    }
}